﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：HW_XXX_User类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-30   
    /// </summary>
    public class HW_XXX_UserService : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~HW_XXX_UserService()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
		#region##MapperHW_XXX_User
        public IList<HW_XXX_User> MapperList(SqlDataReader reader)
         {
            IList<HW_XXX_User> list = new List<HW_XXX_User>();
            HW_XXX_User obj=null;
            while (reader.Read())
            {
                obj = new HW_XXX_User();
                obj.XX_UserName = (string)reader["XX_UserName"];
                obj.RoleId = reader["RoleId"] as int?;
                obj.XX_Pass = reader["XX_Pass"] as string;
                obj.CNName = reader["CNName"] as string;
                obj.ErrorTimes = reader["ErrorTimes"] as int?;
                obj.LastLoginTime = reader["LastLoginTime"] as DateTime?;
                obj.Version = reader["Version"] as int?;
                list.Add(obj);
            }
            return list;
         }
        public HW_XXX_User Mapper(SqlDataReader reader)
         {
            HW_XXX_User obj=null;
            if (reader.Read())
            {
                obj = new HW_XXX_User();
                obj.XX_UserName = (string)reader["XX_UserName"];
                    obj.RoleId = reader["RoleId"] as int?;
                obj.XX_Pass = reader["XX_Pass"] as string;
                obj.CNName = reader["CNName"] as string;
                    obj.ErrorTimes = reader["ErrorTimes"] as int?;
                    obj.LastLoginTime = reader["LastLoginTime"] as DateTime?;
                    obj.Version = reader["Version"] as int?;
            }
            return obj;
         }
        #endregion
        #region##通过主键ID得到HW_XXX_User
        /// <summary>
        /// 功能：通过ID得到HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_XXX_User GetById(string xx_username)
        {            
            string sql = "select * from HW_XXX_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",xx_username)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            return Mapper(reader);
        }
        public int ResetPassword(string xx_username, string pass)
        {
            string sql = "update HW_XXX_User set XX_Pass=@pass";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@pass",pass)
            };
            int result = SqlHelper.ExecuteNonQuery(WebConn.connString, CommandType.Text, sql, sp);
            return result;
        }
        public int ResetPassword(SqlTransaction trans,string xx_username, string pass)
        {
            string sql = "update HW_XXX_User set XX_Pass=@pass where XX_UserName=@xx_username";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@pass",pass),
                new SqlParameter("@xx_username",xx_username)
            };
            int result = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql, sp);
            return result;
        }
        #endregion
		
		#region##通过主键ID删除HW_XXX_User
        /// <summary>
        /// 功能：通过主键ID删除HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string xx_username)
		{
			int i = 0;
			string sql = "delete from HW_XXX_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",xx_username)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,string xx_username)
		{
			int i = 0;
			string sql = "delete from HW_XXX_User where XX_UserName = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",xx_username)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加HW_XXX_User
        /// <summary>
        /// 功能：添加HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_XXX_User hw_xxx_user)
        {
            int result = 0;
            string sql = "insert into HW_XXX_User(XX_UserName,RoleId,XX_Pass,CNName,ErrorTimes,LastLoginTime,Version) values(@XX_UserName,@RoleId,@XX_Pass,@CNName,@ErrorTimes,@LastLoginTime,@Version)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@XX_UserName",hw_xxx_user.XX_UserName),
				new SqlParameter("@RoleId",hw_xxx_user.RoleId),
				new SqlParameter("@XX_Pass",hw_xxx_user.XX_Pass),
				new SqlParameter("@CNName",hw_xxx_user.CNName),
				new SqlParameter("@ErrorTimes",hw_xxx_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_xxx_user.LastLoginTime),
				new SqlParameter("@Version",hw_xxx_user.Version)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,HW_XXX_User hw_xxx_user)
        {
            int result = 0;
            string sql = "insert into HW_XXX_User(XX_UserName,RoleId,XX_Pass,CNName,ErrorTimes,LastLoginTime,Version) values(@XX_UserName,@RoleId,@XX_Pass,@CNName,@ErrorTimes,@LastLoginTime,@Version)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@XX_UserName",hw_xxx_user.XX_UserName),
				new SqlParameter("@RoleId",hw_xxx_user.RoleId),
				new SqlParameter("@XX_Pass",hw_xxx_user.XX_Pass),
				new SqlParameter("@CNName",hw_xxx_user.CNName),
				new SqlParameter("@ErrorTimes",hw_xxx_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_xxx_user.LastLoginTime),
				new SqlParameter("@Version",hw_xxx_user.Version)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到HW_XXX_User集合
		/// <summary>
        /// 功能：得到HW_XXX_User集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
		public IList<HW_XXX_User> GetList()
		{
			string sql = "select * from HW_XXX_User";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
            return MapperList(reader);
		}
		#endregion
		
		#region##修改HW_XXX_User
        /// <summary>
        /// 功能：修改HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_XXX_User(HW_XXX_User hw_xxx_user)
        {
            int result = 0;
            string sql = "update HW_XXX_User set XX_UserName = @XX_UserName,RoleId = @RoleId,XX_Pass = @XX_Pass,CNName = @CNName,ErrorTimes = @ErrorTimes,LastLoginTime = @LastLoginTime,Version = @Version where XX_UserName = @XX_UserName";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@XX_UserName",hw_xxx_user.XX_UserName),
				new SqlParameter("@RoleId",hw_xxx_user.RoleId),
				new SqlParameter("@XX_Pass",hw_xxx_user.XX_Pass),
				new SqlParameter("@CNName",hw_xxx_user.CNName),
				new SqlParameter("@ErrorTimes",hw_xxx_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_xxx_user.LastLoginTime),
				new SqlParameter("@Version",hw_xxx_user.Version)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_XXX_User(SqlTransaction transaction,HW_XXX_User hw_xxx_user)
        {
            int result = 0;
            string sql = "update HW_XXX_User set XX_UserName = @XX_UserName,RoleId = @RoleId,XX_Pass = @XX_Pass,CNName = @CNName,ErrorTimes = @ErrorTimes,LastLoginTime = @LastLoginTime,Version = @Version where XX_UserName = @XX_UserName";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@XX_UserName",hw_xxx_user.XX_UserName),
				new SqlParameter("@RoleId",hw_xxx_user.RoleId),
				new SqlParameter("@XX_Pass",hw_xxx_user.XX_Pass),
				new SqlParameter("@CNName",hw_xxx_user.CNName),
				new SqlParameter("@ErrorTimes",hw_xxx_user.ErrorTimes),
				new SqlParameter("@LastLoginTime",hw_xxx_user.LastLoginTime),
				new SqlParameter("@Version",hw_xxx_user.Version)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计HW_XXX_User
		/// <summary>
        /// 修改HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from HW_XXX_User";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改HW_XXX_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(XX_UserName) from HW_XXX_User";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义HW_XXX_User
        public HW_XXX_User CheckUser(string userName)
        {
            string sql = "select * from HW_XXX_User where XX_UserName=@userName";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@userName",userName),
                //new SqlParameter("@pass",pass)
            };
            SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString, CommandType.Text, sql,sp);
            return Mapper(reader);
        }
        #endregion
                
    }
}
